﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Admin : System.Web.UI.MasterPage
{
    public int MaMenuCha = 0;
    public int MaMenuCon = 0;
    protected override void OnInit(EventArgs e)
    {
        if (Session["Admin"] == null)
            Response.Redirect("~/admin/login.aspx");
        string path = "";
        string[] arrPath = Request.FilePath.Split('/');
        int i = 0;
        for (i = (arrPath.Length-1); i >= 0; i--)
        {
            if (arrPath[i].Equals("Admin")) {
                break;
            }
        }
        for (int j = (i+1); j <= (arrPath.Length - 1); j++)
        {
            if (!path.Equals("")) path += "/";
            path += arrPath[j]; 
        }
        QuanLyINFO QLCurr = (QuanLyINFO)Session["Admin"];
        ChucNangINFO chucnang = QuanLyDAL.KiemTraQuyen(QLCurr.MaQL,path);
        if (chucnang == null || chucnang.MaChucNang <= 0)
            Response.Redirect("~/admin/ErrorPage.aspx");
        MaMenuCha = chucnang.LoaiCha;
        MaMenuCon = chucnang.MaChucNang;
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            load();
        }
    }
    protected void load() {
        QuanLyINFO user = (QuanLyINFO)Session["Admin"];
        lblname1.Text = user.HoTen;
        loadMenu(user.MaQL);
    }

    private void loadMenu(int MaQL) {
        List<ChucNangINFO> dsCha = ChucNangDAL.dsChucNangCha(MaQL);
        List<ChucNangINFO> dsChucNang = ChucNangDAL.dsChucNang(MaQL);
        StringBuilder strMenu = new StringBuilder();
        foreach (var item1 in dsCha)
        {
            strMenu.Append("<li class='treeview' id='item_").Append(item1.MaChucNang).Append("'><a href='javascript:void()'>");
            strMenu.Append("<i class='").Append(item1.Icon).Append("'></i> <span>").Append(item1.TenChucNang).Append("</span></a>");
            strMenu.Append("<ul class='treeview-menu' id='item_ul_").Append(item1.MaChucNang).Append("'>");
            foreach (var item in dsChucNang)
            {
                if(item.LoaiCha == item1.MaChucNang && item.TrangThai == 1)
                strMenu.Append("<li id='item_").Append(item.MaChucNang).Append("'><a href='").Append(ResolveUrl("~/Admin/"+item.LienKet)).Append("' style='margin-left: 10px;'><i class='fa fa-angle-double-right'></i>").Append(item.TenChucNang).Append("</a></li>");
            }
            strMenu.Append("</ul></li>");
        }
        ltrMenu.Text = strMenu.ToString();
    }
}
